package com.pi.pro.xcmg.am.was.web.cust.controller;





import com.pi.common.CodesConstants;
import com.pi.framework.core.controller.BaseCrudController;
import com.pi.framework.core.service.ICrudService;
import com.pi.framework.core.util.SecurityUtils;
import com.pi.framework.core.util.StringUtils;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.pro.xcmg.am.was.fac.service.WasCustomWorkService;
import com.pi.wms.common.WmsConstants;
import com.pi.wms.dto.act.TaskActionDto;
import com.pi.wms.entity.TwtActTaskList;
import com.pi.wms.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Controller
@RequestMapping("/task/patask/ca")
public class CustApplyPaTasklistsController extends BaseCrudController<TwtActTaskList, Long> {

	@Autowired
	protected TaskService service;

	@Autowired
	private WasCustomWorkService wasCustomWorkService;

	@Override
	protected ICrudService<TwtActTaskList, Long> getCrudService() {
	    return service;
	}

	@Override
	protected String getPrefix() {
		return "patask";
	}

	@Override
	protected TwtActTaskList newModel(HttpServletRequest request) {
		return new TwtActTaskList();
	}

	@Override
	protected void prepareDatatableConditions(Map<String, Object> query, DataTablesInput input) {
		Long userId = SecurityUtils.getLoginUserId();
		query.put("userId", userId.toString());
		query.put("tasktype", WmsConstants.TASK_TYPE_PUTAWAY);
		query.put("showcancel", CodesConstants.COMMON_FLAG_NO);
		super.prepareDatatableConditions(query, input);
	}

	@PostMapping("confirm")
	public String confirm(@RequestParam("ids") Long[] ids, Model model, HttpServletRequest request,
			RedirectAttributes redirectAttributes) {
		model.addAttribute("params", getParams(request));
		String m="";
		boolean failed=false;
		TwtActTaskList task=null;
		if ((ids == null) || (ids.length==0)) {
            addSuccessMessage(redirectAttributes, "确认上架成功");
			return this.redirect("");
		}
		Map<String,String> nums=new HashMap<>();
		List<TwtActTaskList> tasks=new ArrayList<>();
        for (Long id : ids) {
            task = service.findById(id);
            if (task == null) {
                continue;
            }
            if (nums.containsKey(task.getNumber())){
                continue;
            }
            tasks.add(task);
            nums.put(task.getNumber(), CodesConstants.PROCESS_FAIL_FAST);
        }
        if ((task==null)||(tasks.size()==0)) {
            addSuccessMessage(redirectAttributes, "确认上架成功");
            return this.redirect("");
        }
        TaskActionDto actionDto=new TaskActionDto();
        actionDto.setDivisionCode(XcmgAmConstants.DIVISION_WH101_CODE);
        actionDto.setWorker(SecurityUtils.getLoginUsername());
        m =  this.wasCustomWorkService.taskFinish(tasks, "", CodesConstants.SYSTEM_WMS, actionDto);
        if(!StringUtils.isNullOrBlank(m)){
            addErrorMessage(redirectAttributes, "确认上架失败"+m);
            return this.redirect("");
        }
        addSuccessMessage(redirectAttributes, "确认上架成功");
		return this.redirect("");
	}

	//
	protected String redirect(String relativeUrl) {
		if(!StringUtils.isNullOrBlank(relativeUrl) && !relativeUrl.startsWith("/")) {
			relativeUrl = "/" + relativeUrl;
		}
		return "redirect:" + this.referUrl() + relativeUrl;
	}

	private String referUrl(){
		return "/task/patask/";
	}


}
